FROM php:8.0.27-cli-alpine3.16
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

ENV TIMEZONE=${timezone:-"Asia/Shanghai"} \
	APP_ENV=prod \
    SCAN_CACHEABLE=(true) \
    APP_SYSTEM_ENV=docker \
	COMPOSER_CDN=https://mirrors.aliyun.com/composer/
	# \
    # Setup bento4 environment variables
    #PATH=/usr/local/bin/bento4/bin:${PATH}

# PHPIZE_DEPS 中已包含 autoconf dpkg-dev dpkg file g++ gcc libc-dev make pkgconf re2c

RUN \
    curl -sfL https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer \
    && chmod +x /usr/bin/composer \
    && composer self-update --clean-backups 2.3.10 \
	&& composer config -g repo.packagist composer ${COMPOSER_CDN} \
    && apk update \
    # 可选 python3 库
    && apk add --no-cache git expect openssh-client openssl tesseract-ocr bash ffmpeg libzip  \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS cmake libstdc++ openssl-dev zlib zlib-dev gmp-dev libzip-dev curl-dev pcre-dev pcre2-dev libtool linux-headers build-base freetype-dev libjpeg-turbo-dev libpng-dev yasm \
	# download
	&& git clone https://gitee.com/lujihong/docker.git /tmp/docker \
	&& ls -alh \
	# create /usr/src/php/ext
	&& docker-php-source extract \
    # move files
    && mv /tmp/docker/swoole-src-5.0.2 /usr/src/php/ext/swoole \
    && mv /tmp/docker/mongodb /usr/src/php/ext/mongodb \
    && mv /tmp/docker/inotify /usr/src/php/ext/inotify \
    && mv /tmp/docker/xlswriter-1.5.2 /usr/src/php/ext/xlswriter \
    && mv /tmp/docker/xdebug-3.2.0 /usr/src/php/ext/xdebug \
    #https://xdebug.org/files/xdebug-3.2.0.tgz

	# install ext
	&& pecl update-channels \
	&& pecl install redis \
    && docker-php-ext-enable redis \
    && docker-php-ext-install -j$(nproc) gmp \
    && docker-php-ext-configure zip --with-zip --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install zip \
	&& docker-php-ext-install sockets \
	&& docker-php-ext-install bcmath \
	&& docker-php-ext-install ctype \
	&& docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg \
	&& docker-php-ext-install -j$(nproc) gd \
	&& docker-php-ext-install pdo_mysql \
	&& docker-php-ext-install pcntl \
	&& docker-php-ext-install opcache \
	&& docker-php-ext-configure sysvshm \
		--with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvshm \
	&& docker-php-ext-configure sysvmsg \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvmsg \
	&& docker-php-ext-configure sysvsem \
	    --with-php-config=/usr/local/bin/php-config \
	&& docker-php-ext-install sysvsem \

	# swoole
	&& docker-php-ext-configure swoole \
	    #--enable-http2 --enable-swoole-json \ #swoole5.0 已移除这两个配置
	    --enable-mysqlnd \
	    --enable-openssl \
	    --enable-sockets --enable-swoole-curl \
	&& docker-php-ext-install -j$(nproc) swoole \
	
	# mongodb
	&& docker-php-ext-configure mongodb \
	    --enable-mongodb \
	&& docker-php-ext-install -j$(nproc) mongodb \
	
	# inotify
	&& docker-php-ext-configure inotify \
	    --enable-inotify \
	&& docker-php-ext-install inotify \
	
	# xlswriter
	&& docker-php-ext-configure xlswriter \
	    --enable-reader \
	&& docker-php-ext-install -j$(nproc) xlswriter \

	# xdebug
	&& docker-php-ext-configure xdebug \
	    --enable-xdebug \
	&& docker-php-ext-install -j$(nproc) xdebug \
    && rm -rf /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \

	# Build bento4
	#&& cd /tmp/docker/bento4 \
	#&& ls -alh \
	#&& rm -rf /tmp/docker/bento4/cmakebuild \
	#&& mkdir -p /tmp/docker/bento4/cmakebuild/x86_64-unknown-linux \
	#&& cd /tmp/docker/bento4/cmakebuild/x86_64-unknown-linux \
	#&& cmake -DCMAKE_BUILD_TYPE=Release ../.. \
	#&& make \
	
	# Install bento4
	#&& cd /tmp/docker/bento4 && python3 Scripts/SdkPackager.py x86_64-unknown-linux . cmake \
	#&& mkdir /usr/local/bin/bento4 \
	#&& mv /tmp/docker/bento4/SDK/Bento4-SDK-*.x86_64-unknown-linux/* /usr/local/bin/bento4 \
	
	#  ---------- some config ----------
	&& echo "opcache.enable_cli = 'On'" >> /usr/local/etc/php/conf.d/docker-php-ext-opcache.ini \
	&& echo "swoole.use_shortname = 'Off'" >> /usr/local/etc/php/conf.d/docker-php-ext-swoole.ini \
    && echo ";zend_extension=xdebug.so" > /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
	&& echo "xdebug.cli_color=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
	&& echo "xdebug.discover_client_host=0" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
	#,trace
    && echo "xdebug.mode=develop,debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
	&& echo "xdebug.client_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
	&& echo "xdebug.start_with_request=yes" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \

	# config PHP
	&& cd /usr/local/etc/php \
	&& { \
	    echo "upload_max_filesize=128M"; \
	    echo "post_max_size=128M"; \
	    echo "memory_limit=1G"; \
	    echo "date.timezone=${TIMEZONE}"; \
	} | tee conf.d/99_overrides.ini \
	
	# config timezone
	&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
	&& echo "${TIMEZONE}" > /etc/timezone \
	
	# php info
	&& php -v \
	&& php -m \
	&& php --ri swoole \
	&& composer \
	
	# clear
	&& docker-php-source delete \
	&& apk del .build-deps \
	&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man /usr/src/php.tar.xz* $HOME/.composer/*-old.phar \
	&& echo -e "\033[42;37m Build Completed :).\033[0m\n"

WORKDIR "/opt/www/"

COPY . /opt/www

# Composer Cache
# COPY ./composer.* /opt/www/
# RUN composer install --no-dev --no-scripts

#EXPOSE 9501 9502

ENTRYPOINT ["php", "/opt/www/bin/hyperf.php", "start"]